// 统计报表部分的接口
import Mock from 'mockjs'
import {param2Obj} from '../utils/urlutils.js'
let List = [];
let length = 100;
for (var i = 0; i < length; i++) {
    List.push(
        {
            id:Mock.mock('@guid'),
            state: 1,
            type: Mock.mock({
                "array|1": [
                  "待办",
                  "处方开立",
                  "开立中",
                ]
              }).array,
            orderNum: Mock.mock('@id'), // 100130
            name: Mock.mock('@cname()'),
            department: "内科",
            docter: Mock.mock('@cname()'),
            discount: Mock.mock('@natural(40, 300)'),
            receive: 10,
            trueRec: 200,
            yb: 100,
            vipcard: 0,
            cash: 0,
            zfb: 0,
            wx: 0,
            card: 0,
            date: Mock.mock('@date("yyyy-MM-dd")'),
            tollCollector: Mock.mock('@cname()'),
        }
    );
}

export default {
    getStaticData(config) {
        var {keyword,page,pageSize,orderType,fromDate,toDate} = param2Obj(config.url);

        console.log(keyword,page,pageSize,orderType,fromDate,toDate);
        console.log("getStaticData:",config.url);
        var res = List.filter(item=>{
            return (item.name.indexOf(keyword)!=-1 || item.orderNum.indexOf(keyword)!=-1);
        })     
        // console.log("筛选了名字的长度：",res.length);
        if(orderType != '' ){
            // console.log("筛选类型：");
            res = res.filter(item=>{
                return item.type == orderType;
            })
        }
        // 筛选在指定日期的
        if(fromDate &&toDate &&fromDate!='' && toDate!='')
        {
            // console.log("筛选日期？");
            res = res.filter(item=>{
                let val = false;
                let [dY,dM,dD] = item.date.split('-');
                let [fY,fM,fD] = fromDate.split('-');
                let [tY,tM,tD] = toDate.split('-');
                if(dY>=fY && dY<=tY)
                {   
                    if(dM>=fM && dM<=tM)
                    {
                        if(dD>=fD && dD<=tD)
                        {
                            val = true;
                        }else
                        {
                            val = false;
                        }
                    }else
                    {
                        val=false;
                    }
                }else
                    val = false;
                return val;
            });
        }

        // 分页筛选
        var resLimit = res.filter((item,index)=>{
            return (page-1)*pageSize<=index && index<page*pageSize;
        });

        return {
            code: 200,
            message: "getStaticData - 获取数据成功",
            data: resLimit,
            total:res.length
        }
    },
    editStaticData(config)
    {
        var {id,name,receive,state,tollCollector,trueRec,cash,type,vipcard,wx,yb,zfb,docter,date,
        department,card,discount} = JSON.parse(config.body);
        var index = List.findIndex(item=>item.id==id);
        if(index==-1)
        {
            return {
                code: 201,
                message: `查找数据失败,不存在id为${id}的数据`,
                data: null,
            }
        }
        List[index].cash = cash;
        List[index].discount = discount;
        List[index].card = card;
        List[index].department = department;
        List[index].name = name;
        List[index].receive = receive;
        List[index].state = state;
        List[index].tollCollector = tollCollector;
        List[index].trueRec = trueRec;
        List[index].type = type;
        List[index].vipcard = vipcard;
        List[index].wx = wx;
        List[index].yb = yb;
        List[index].zfb = zfb;
        List[index].docter = docter;
        List[index].date = date;
        return {
            code: 200,
            message: "editStaticData - 修改数据成功",
            data: null,
        }
    }
}